//
//  WishViewController.m
//  LiuHeBao
//
//  Created by zr on 2018/8/22.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "WishViewController.h"
#import "WishHeader.h"
#import "WishTableViewCell.h"
#import "NewWishTableViewCell.h"
#import "WeiteTieZiViewController.h"
#import "WishInfoViewController.h"
#import "HopeModel.h"

@interface WishViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray <HopeModel *> * modelArray;
@property (nonatomic, copy)NSString * type;

@end

@implementation WishViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = WHITE_COLOR;
    self.type = @"one";
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeWishData:) name:@"changeWishData" object:nil];
    
    [self loadDataWithDict:nil];
    [self setupUI];
}


- (void)loadDataWithDict:(NSDictionary *)dict{
    
    [HopeModel hopeModelWithDict:dict WithSucess:^(NSArray<HopeModel *> *array) {
        
        self.modelArray = array;
    } error:^{
        
    }];
}

- (void)setModelArray:(NSArray<HopeModel *> *)modelArray{
    
    _modelArray = modelArray;
    
    [self.tableView reloadData];
}

- (void)setupUI{
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT - 64 - 45 * SCREENH6) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor whiteColor];
//    tableView.rowHeight = 160 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[WishTableViewCell class] forCellReuseIdentifier:@"WishListCell"];
    [self.tableView registerClass:[NewWishTableViewCell class] forCellReuseIdentifier:@"NewWishListCell"];
    [self.tableView registerClass:[WishHeader class] forHeaderFooterViewReuseIdentifier:@"WishHeaderView"];
    
    [self.view addSubview:self.tableView];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.modelArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.type isEqualToString:@"two"]) {
        
        NewWishTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewWishListCell" forIndexPath:indexPath];
        
        cell.model = self.modelArray[indexPath.row];
        
        return cell;
        
    }else{
        
        WishTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WishListCell" forIndexPath:indexPath];
        
        cell.model = self.modelArray[indexPath.row];
        
        return cell;
    }
    
   
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.type isEqualToString:@"two"]) {
        
        return 94 * SCREENH6;
        
    }else{
        
        return 160 * SCREENH6;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 45 * SCREENH6;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    WishHeader * header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"WishHeaderView"];
    
    return header;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HIDDEN_TAB;
    
    HopeModel * model = self.modelArray[indexPath.row];
    
    WishInfoViewController * infoVC = [WishInfoViewController new];
    
    infoVC.ID = model.ID;
    
    [self.navigationController pushViewController:infoVC animated:YES];
    
    SHOW_TAB;
}

- (void)changeWishData:(NSNotification *)noti{
    
    self.type = noti.userInfo[@"type"];
    
    if ([self.type isEqualToString:@"add"]) {
        
        HIDDEN_TAB;
        
        WeiteTieZiViewController * writeVC = [WeiteTieZiViewController new];
        
        [self.navigationController pushViewController:writeVC animated:YES];
        
        SHOW_TAB;
    }else if ([self.type isEqualToString:@"one"]){
        
        [self loadDataWithDict:nil];
    }else if ([self.type isEqualToString:@"two"]){
        
        NSDictionary * dict = @{@"order":@"time"};
        
        [self loadDataWithDict:dict];
    }else if ([self.type isEqualToString:@"three"]){
        
        NSDictionary * dict = @{@"order":@"comment"};
        
        [self loadDataWithDict:dict];
    }
}


//- (void)setType:(NSString *)type{
//
//    _type = type;
//
//    [self.tableView reloadData];
//
//}

- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


@end
